var ns, UIAlbumBrowser, bmLocation, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID: 0,
            pageindex: 0,
            pagesize: 10,
            PlanType: 5,//消息类型 1-设备管理(巡检、维保 )2-巡检 3-维保 4-巡更 5-保洁
            TaskStatus: 1,//0-未开始,1-进行中,2-已结束
            planTypeList: [5],
        },
        total: {
            ProcessCount: 0,
        },
        current_item_length: 0,
        scroll_top: 0,
        can_scroll: false,
        showTop: false,
        showsearch: false,
        number: 0,
        selectlist: [],
        choosetaskmodel: {},
        optionName: '',
        choosesortmodel: {},
        option2: [],
        option3: [
            // { text: '排序', value: 0 },
            { text: '时间近到远', value: 1 },
            { text: '时间远到近', value: 2 },
        ],
        OrderByType: 0,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.can_scroll = false;
            var options = {};
            options.action = "getdeviceplantasklist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.Type = that.form.PlanType;
            options.Status = that.form.TaskStatus;
            options.OrderByType = that.OrderByType;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {

                    if (data.listHandle.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.selectlist = data.listHandle;
                    } else {
                        that.selectlist = that.selectlist.concat(data.listHandle);
                    }
                    app.current_item_length = that.selectlist.length
                } else if (err) {
                    // that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //获取筛选条件
        get_menuType: function () {
            var that = this;
            var options = {};
            options.action = 'getdeviceplantasksearchparams';
            options.Type = 5;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        that.option2 = data.list;
                        that.choosetaskmodel = data.list[1];
                        that.optionName = that.choosetaskmodel.Name + '(' + that.choosetaskmodel.Number + ')';
                        that.choosesortmodel = that.option3[0];
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },

        onChooseTask: function (item) {
            var that = this;
            that.choosetaskmodel = item;
            that.optionName = that.choosetaskmodel.Name + '(' + that.choosetaskmodel.Number + ')';
            this.$refs.choosetask.toggle();
            that.form.TaskStatus = item.Type;
            that.get_data();
        },
        onChooseSort: function (item) {
            var that = this;
            that.choosesortmodel = item;
            this.$refs.choosesort.toggle();
            that.OrderByType = item.value;
            that.get_data();
        },

        select_status: function (status) {
            var that = this;
            that.showTop = false;
            that.form.TaskStatus = status;
            that.get_data();
        },
        open_list: function (item) {
            var that = this;
            var url = 'bjlh_list_frm';
            var title = item.DevicePlanName;
            // if (item.TaskStatus == 1) {
            //     url = 'bjlh_summary_frm';
            //     title = '任务详情';
            // }
            ns.openWin(url, title, {
                id: item.ID
            });
        },
        open_add: function () {
            var that = this;
            ns.openWin('bjlh_addfrm', '新增记录');
        },
        doHideSearch: function () {
            var that = this;
            that.showTop = false;
            that.showsearch = false;
        },
        select_allstatus: function () {
            var that = this;
            that.showsearch = !that.showsearch;
            var options = {};
            options.planTypeList = JSON.stringify(that.form.planTypeList);
            options.action = "getdeviceplantaskpendinghandlelist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.selectlist = data.list;

                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });

        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.get_menuType();
    app.form.status = api.pageParam.status || 1;
    app.form.ID = api.pageParam.ID || 0;
    setTimeout(function () {
        app.form.status = 1;
        app.get_data();
        setTimeout(function () {
            ns.init_pull();
        }, 500)
    }, 500)
    api.addEventListener({
        name: 'devicetaskcomplete'
    }, function (ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'startplantaskdone'
    }, function (ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'do_open_add'
    }, function (ret, err) {
        app.open_add();
    });
};
